<template>
  <el-dialog :title="title" :visible="showDialog" @close="closeAaa">
    <el-form ref="deptForm" label-width="120px" :model="from" :rules="rules">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="from.name" style="width:80%" />
      </el-form-item>
      <el-form-item label="角色描述" prop="description">
        <el-input v-model="from.description" style="width:80%" />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button type="primary" size="small" @click.native="clicksubmit">确定</el-button>
      <el-button size="small" @click.native="delClick">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addsetList, getxiugaiAPI, ReviseAPI } from '@/api/setting'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    setAddlist: {
      type: Array,
      default: () => []
    },
    getlist: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      from: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' },
          { validator: this.checkNameRepeat, trigger: 'blur' }],
        description: [{ required: true, message: '名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    title() {
      return this.from.id ? '编辑弹层' : '新增弹层'
    }
  },
  methods: {
    closeAaa() {
      this.$emit('update:showDialog', false)
      this.delClick()
      this.from.id = ''
    },
    /**
     * 兜底校验，添加角色
     */
    clicksubmit() {
      this.$refs.deptForm.validate(async(valid) => {
        if (!valid) return
        if (this.from.id) {
          await ReviseAPI(this.from)
          // console.log(res)
          this.$message.success('修改成功')
        } else {
          await addsetList(this.from)
          this.$message.success('添加成功')
          // console.log(this.$parent)
        }
        this.$parent.settingList()
        this.closeAaa()
      })
    },
    /**
     * 点击取消按钮完成重置
     */
    delClick() {
      this.$refs.deptForm.resetFields()
    },
    /**
     * 弹出层回显并修改
     */
    async getxiugai(data) {
      this.from.id = data.id
      const { data: res } = await getxiugaiAPI(data.id)
      // console.log(data)
      // console.log(res)
      this.from = res
    }
  }
}
</script>

<style lang="scss" scoped>
.footer{
  display: flex;
 justify-content: center;
}
</style>
